#ifndef PROJECTZ_NUSER_NCOMMAND_CS_REQ_CHANGE_MAIN_CHARACTER_H_
#define PROJECTZ_NUSER_NCOMMAND_CS_REQ_CHANGE_MAIN_CHARACTER_H_

#include "../../engine.h"

#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Equip.h"

namespace ProjectZ { namespace NCommand { namespace NWorld {

class CS_REQ_CHANGE_MAIN_CHARACTER
{
public:
	static void OnExecute(User * pUser, ::Network::Packet * pPacket)
	{
		PREPARE_PACKET;

		//int slot_number = 0;
		int slot_number = -1;
		pPacket->ParseMsg("%c", &slot_number);

		DEBUGLOG("CS_REQ_CHANGE_MAIN_CHARACTER slot_number : %d", slot_number);

		if (slot_number < 0 || slot_number > 7)
		{
			DEBUGLOG("CS_REQ_CHANGE_MAIN_CHARACTER slot_number : %d", slot_number);
			SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
			return;
		}

		DEBUGLOG("CS_REQ_CHANGE_MAIN_CHARACTER slot_number : %d character_seq : %d GID : %d",
				slot_number, pUser->GetUserInfo().array_Slot[slot_number].character_seq,
				pUser->GetUserSeq());
		if (0 == pUser->GetUserInfo().array_Slot[slot_number].character_seq)
		{
			SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
			return;
		}

		if (false == pUser->GetUserInfo().array_Slot[slot_number].makeCharacter)
		{
			SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
			return;
		}

		pUser->GetUserInfo().main_slot_index = slot_number;

		// 새로운 캐릭터의 데이터를 읽어 오기 위해 Bag정보 초기화
		for(int i = static_cast<int>(BAG_TYPE_NORMAL); i < static_cast<int>(BAG_TYPE_MAX); ++i)
		{
			NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(i));
			if (NULL == pBag)
			{
				SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
				return;
			}
			pBag->Finalize();
		}
		pUser->CacheBagInfo(false);

		NLogic::Equip * pEquip = pUser->GetEquip();
		if (NULL == pEquip)
		{
			SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
			return;
		}
		pEquip->Finalize();


		//pUser->GetCharacterInfo().level = pUser->GetCharacterInfoFromIndex(slot_number).level;
		//pUser->GetCharacterInfo().exp = pUser->GetCharacterInfoFromIndex(slot_number).exp;

		pUser->ChangeMainSlot();

		SendPacket(pUser, ACK_OK, slot_number);

		NProxy::SetMainCharacterInfo(pUser);
	}

	static void SendPacket(User * pUser, int res, int slot_number)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

		pAck->Write2(res);
		pAck->Write1(slot_number);
		pAck->MakePacket(CMD_SC_REQ_CHANGE_MAIN_CHARACTER);

		pUser->Send(pAck);
	}
};

} /*NWorld*/ } /*NCommand*/ } /*ProjectZ*/


#endif /* PROJECTZ_NUSER_NCOMMAND_CS_REQ_CHANGE_MAIN_CHARACTER_H_ */
